﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MediaBrowser.CustomControls.CustomEvent;

namespace MediaBrowser.CustomControls.ScrollControl
{
    /// <summary>
    /// Logique d'interaction pour ScrollButton.xaml
    /// </summary>
    public partial class ScrollButton : UserControl
    {
        Point m_lastMousePoint;
        double cursorScrollLeft;
        public ScrollButton()
        {
            InitializeComponent();
            cursorScrollLeft = Canvas.GetLeft(CursorScroll);
        }

        /// <summary>
        /// Evenement du déplacement du Curseur
        /// </summary>
        private void RaiseEventValueChange()
        {
            double currentPos = Canvas.GetLeft(CursorScroll);
            double currentValue = 0;

            if (currentPos < cursorScrollLeft)
            {
                double maxCursor = cursorScrollLeft - Canvas.GetLeft(ContainerCursorScroll);
                double currentCursor = Canvas.GetLeft(CursorScroll) - Canvas.GetLeft(ContainerCursorScroll);
                currentValue = -(100 - (100 * currentCursor) / maxCursor);
            }
            else if (currentPos > cursorScrollLeft)
            {
                double maxContainer = Canvas.GetLeft(ContainerCursorScroll) + ContainerCursorScroll.ActualWidth;
                double maxCursor = maxContainer - (cursorScrollLeft + CursorScroll.ActualWidth);
                double currentCursor = maxContainer - (currentPos + CursorScroll.ActualWidth);
                currentValue = 100 - (100 * currentCursor) / maxCursor;
            }
            ValueChangeEventArgs args = new ValueChangeEventArgs(ValueChangeEvent, this, currentValue);
            RaiseEvent(args);
        }

        /// <summary>
        /// Quand la souris Quitte le contrôle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonScroller_MouseLeave(object sender, MouseEventArgs e)
        {
            AnimateCursorScrollCenter();
        }

        /// <summary>
        /// Lancement Animation Curseur Bouton Droite Gauche 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (sender.Equals(ButtonL))
            {
                Canvas.SetLeft(CursorScroll, cursorScrollLeft - 10);
                RaiseEventValueChange();

            }
            else
            {
                Canvas.SetLeft(CursorScroll, cursorScrollLeft + 10);
                RaiseEventValueChange();
            }
        }

        /// <summary>
        /// Mouse UP, Lancement Animation : Curseur reviens au centre
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void On_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AnimateCursorScrollCenter();
        }

        /// <summary>
        /// Lancement Animation au clique sur le Container Cursor
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ContainerCursorScroll_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point mouseCurrent = e.GetPosition(MainCanvas);

                if (mouseCurrent.X <= Canvas.GetLeft(CursorScroll))
                {
                    if (mouseCurrent.X >= 20)
                    {
                        Canvas.SetLeft(CursorScroll, mouseCurrent.X - 2);
                        RaiseEventValueChange();
                    }
                }
                else
                {
                    if (((mouseCurrent.X) - CursorScroll.Data.Bounds.Width) <= 80.4)
                    {
                        Canvas.SetLeft(CursorScroll, (mouseCurrent.X + 2) - CursorScroll.Data.Bounds.Width);
                        RaiseEventValueChange();
                    }
                }
            }
        }

        #region Mouse Move Cursor

        private void CursorScroll_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            UIElement ele = sender as UIElement;
            ele.CaptureMouse();
            m_lastMousePoint = e.GetPosition(null);
            ele.PreviewMouseMove += CursorScroll_MouseMove;
            ele.PreviewMouseLeftButtonUp += CursorScroll_MouseLeftButtonUp;
        }

        private void CursorScroll_MouseMove(object sender, MouseEventArgs e)
        {
            Point newPoint = e.GetPosition(null);
            UIElement ele = sender as UIElement;

            // Get change in mouse coords.
            var changeInX = newPoint.X - m_lastMousePoint.X;

            var oldLeft = (double)ele.GetValue(Canvas.LeftProperty);

            // Determine new based on old and the change in mouse move.
            if ((oldLeft + changeInX >= 18.4) && (oldLeft + changeInX <= 81))
            {
                ele.SetValue(Canvas.LeftProperty, oldLeft + changeInX);
            }
            else if (oldLeft + changeInX > 81)
            {
                ele.SetValue(Canvas.LeftProperty, 82.0);
            }
            else if (oldLeft + changeInX > 18.4)
            {
                ele.SetValue(Canvas.LeftProperty, 18.1);
            }
            m_lastMousePoint = newPoint;
            RaiseEventValueChange();
        }

        private void CursorScroll_MouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            UIElement ele = sender as UIElement;
            ele.PreviewMouseMove -= CursorScroll_MouseMove;
            ele.PreviewMouseLeftButtonUp -= CursorScroll_MouseLeftButtonUp;
            ele.ReleaseMouseCapture();
            AnimateCursorScrollCenter();
        }

        #endregion

        #region ANIMATION

        /// <summary>
        /// Animation Curseur vers le center
        /// </summary>
        private void AnimateCursorScrollCenter()
        {
            Canvas.SetLeft(CursorScroll, cursorScrollLeft);
            RaiseEventValueChange();
        }

        #endregion

        #region Created Events

        public static readonly RoutedEvent ValueChangeEvent =
            EventManager.RegisterRoutedEvent(
            "OnValueChangeEvent", RoutingStrategy.Bubble,
            typeof(ValueChangeEventHandler),
            typeof(ScrollButton));

        //add remove handlers
        public event ValueChangeEventHandler OnValueChangeEvent
        {
            add { AddHandler(ValueChangeEvent, value); }
            remove { RemoveHandler(ValueChangeEvent, value); }
        }

        #endregion
    }
}
